package com.common.dao.base.service;


import com.common.dao.base.dao.BaseDao;
import com.common.dao.base.entity.Page;
import com.common.dao.base.entity.QueryBase;
import com.common.dao.util.PageUtil;
import com.common.dao.util.RedisCache;
import com.github.pagehelper.PageHelper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class  BaseService<T extends QueryBase> {

    protected abstract BaseDao<T> getDao();

     /**
     * 功能描述：验证数据是否已经存在
     * @param entity
     * @return
     */
     public boolean isExist(T entity){
         return getDao().isExist(entity)>0;
     }

    /**
     * 根据主键值获取对象
     *
     * @param entity
     */
    public T get(T entity) {
        return getDao().selectByPrimaryKey(entity);
    }

    /**
     * 获取全部实体
     */
    public List<T> loadAll() {
        return getDao().selectAll();
    }

    /**
     * 保存
     *
     * @param entity 保存对象
     * @return boolean
     * @throws Exception
     */
    public boolean save(T entity) throws Exception {
        return getDao().insert(entity) > 0;
    }

    /**
     * 更新
     *
     * @param entity 修改对象
     * @return boolean
     * @throws Exception
     */
    public boolean update(T entity) throws Exception {
        return getDao().updateByPrimaryKey(entity)>0;
    }

    /**
     * 删除
     *
     * @param entity 删除对象
     * @return boolean
     * @throws Exception
     */
    public boolean delete(T entity) throws Exception {
        return getDao().deleteByPrimaryKey(entity)>0;
    }

    /**
     * 功能描述：批量删除数据
     *
     * @param entityList
     * @return
     */
    public boolean removeBath(List<T> entityList) throws Exception {
        for (T t : entityList) {
            if (!delete(t)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 分页查询
     *
     * @param queryModel 查询条件
     */
    public Page findByPage(T queryModel){
        PageHelper.startPage(queryModel.getCurrent(), (queryModel.getPageSize() > 0 && queryModel.getPageSize() <= 500) ? queryModel.getPageSize() : 20);
        HashMap<String, Object> res = PageUtil.getResult(getDao().select(queryModel));
        return new Page((List)res.get("rows"),(long)res.get("total"));
    }

    /**
     * 功能描述：通用的查询方法
     * @param query
     * @return
     */
    public Page commonQueryList(Map<String,Object> query){
        int current = (int)query.get("current");
        int pageSize = (int)query.get("pageSize");
        PageHelper.startPage(current, ( pageSize > 0 && pageSize <= 500) ? pageSize : 20);
        HashMap<String, Object> res = PageUtil.getResult(getDao().commonQueryList(query));
        return new Page((List)res.get("rows"),(long)res.get("total"));
    }

    /**
     * 功能描述:获取当前登陆的用户的信息
     * @param redisCache
     * @return
     */
    public Map<String,Object> getUser(RedisCache redisCache){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token  = request.getHeader("x-access-token");
        if(token == null){
            return null;
        }
        return redisCache.getObject(token,Map.class);
    }

}
